import { objectType, extendType, stringArg } from "@nexus/schema";

export const Folder = objectType({
  name: "Folder",
  definition(t) {
    t.string("id");
    t.string("name");
    t.field("project", { type: "Project" });
    t.field("files", { nullable: true, type: "File", list: true });
    t.field("parent", { nullable: true, type: "Folder" });
    t.field("childrens", { nullable: true, type: "Folder", list: true });
  },
});

export const FolderQuery = extendType({
  type: "Query",
  definition(t) {
    t.field("folder", {
      type: "Folder",
      args: {
        folderId: stringArg({ required: true }),
      },
      resolve(__root, args, ctx) {
        return ctx.db.folder.findOne({
          where: { id: args.folderId },
          include: {
            parent: true,
            project: true,
            files: true,
            childrens: true,
          },
        });
      },
    });
  },
});

// query folders by project
export const FolderByProjectQuery = extendType({
  type: "Query",
  definition(t) {
    t.field("folderByProject", {
      type: "Folder",
      list: true,
      args: {
        projectId: stringArg({ required: true }),
      },
      async resolve(__root, args, ctx) {
        const folder_result = await ctx.db.folder.findMany({
          where: { projectId: args.projectId },
          include: {
            parent: true,
            files: true,
            childrens: true,
          },
        });
        return folder_result;
      },
    });
  },
});

export const FoldersQuery = extendType({
  type: "Query",
  definition(t) {
    t.field("folders", {
      type: "Folder",
      list: true,
      resolve(__root, args, ctx) {
        return ctx.db.folder.findMany({
          include: {
            parent: true,
            project: true,
            files: true,
            childrens: true,
          },
        });
      },
    });
  },
});

export const CreateFolderMutation = extendType({
  type: "Mutation",
  definition(t) {
    t.field("createFolder", {
      type: "Folder",
      args: {
        name: stringArg({ required: true }),
        parentId: stringArg(),
        projectId: stringArg({ required: true }),
      },
      resolve(_root, args, ctx) {
        if (args.parentId === null || args.parentId === undefined) {
          const folder = {
            name: args.name,
            project: { connect: { id: args.projectId } },
          };
          return ctx.db.folder.create({ data: folder });
        } else {
          const folder = {
            name: args.name,
            parent: { connect: { id: args.parentId } },
            project: { connect: { id: args.projectId } },
          };
          return ctx.db.folder.create({ data: folder });
        }
      },
    });
  },
});

export const DeleteFolderMutation = extendType({
  type: "Mutation",
  definition(t) {
    t.field("deleteFolder", {
      type: "Folder",
      args: {
        folderId: stringArg({ required: true }),
      },
      resolve(_root, args, ctx) {
        return ctx.db.folder.delete({
          where: { id: args.folderId },
        });
      },
    });
  },
});
